# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
# listNode就是head 先反转在打印

class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        # 反转链表在输出
        # 输出之后反转列表[::-1]
        if listNode is None:
            return []
        cur = listNode[0]
        pre = None
        tmp = cur
        res = []
        for _ in range(len(listNode)):
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        while listNode:
            res.append(listNode.val)
        return res
# 打印好的列表反转
class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        # 反转链表在输出
        # 输出之后反转列表[::-1]
        if listNode is None:
            return []
        res = []
        fRes = []
        while listNode:
            res.append(listNode.val)
            listNode = listNode.next
        while res:
           fRes.append(res.pop())
        return fRes
# 更简单的写法
class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        # 反转链表在输出
        # 输出之后反转列表[::-1]
        res = []
        while listNode:
            res.append(listNode.val)
            listNode = listNode.next
        return res[::-1]